package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.dishMapper;
import com.sky.mapper.setMealMapper;
import com.sky.mapper.shopCarMapper;
import com.sky.service.shopCarService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
public class shopCarServiceable implements shopCarService {
    @Autowired
    private setMealMapper setMealMapper;
    @Autowired
    private shopCarMapper shopCarMapper;
    @Autowired
    private dishMapper dishMapper;

    @Override
    public void addService(ShoppingCartDTO shoppingCartDTO) {
        LocalDateTime now = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();
        //添加套餐
        String name = "";
        String image = "";
        BigDecimal price;
        int number = 1;
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(currentId);
        //System.out.println(BaseContext.getCurrentId());
        if (shoppingCartDTO.getSetmealId() != null) {
            Integer setMealId = Math.toIntExact(shoppingCartDTO.getSetmealId());
            //取出相印的套餐信息

            //先查看数据库表中是否有该套餐
            ShoppingCart i = shopCarMapper.select(shoppingCart);
            //存在，将数量加一，价格翻倍.更新购物车该项套餐
            if (i != null) {
                //口味是否改变
                //if (extracted(shoppingCartDTO, i, price, number)) return;
                //求出单价
                //price=i.getAmount().divide(BigDecimal.valueOf(i.getNumber()));
                //价格和数量变化
                i.setNumber(i.getNumber() + 1);
                i.setCreateTime(now);
                //i.setAmount(price.multiply(BigDecimal.valueOf(i.getNumber())));
                shopCarMapper.updateShop(i);
                return;
            }
            SetmealVO setmealVO = setMealMapper.selectById(setMealId);
            price = setmealVO.getPrice();
            name = setmealVO.getName();
            image = setmealVO.getImage();
            ShoppingCart build = ShoppingCart.builder().amount(price).
                    image(image).createTime(now).userId(currentId).
                    setmealId(Long.valueOf(setMealId)).
                    number(number).name(name).build();
            shopCarMapper.insert(build);
            return;
        }
        //菜品添加
        //取出相印的套餐信息
        Integer dishId = Math.toIntExact(shoppingCartDTO.getDishId());
        String dishFlavor = shoppingCartDTO.getDishFlavor();
        shoppingCart.setDishFlavor(dishFlavor);
        //先查看数据库表中是否有该菜品
        ShoppingCart i = shopCarMapper.select(shoppingCart);
        //存在，将数量加一，价格翻倍.更新购物车该项套餐
        if (i != null) {
            //口味是否改变
            //if (extracted(shoppingCartDTO, i, price, number)) return;
            //求出单价
            //price=i.getAmount().divide(BigDecimal.valueOf(i.getNumber()));
            //价格和数量变化
            i.setNumber(i.getNumber() + 1);
            i.setCreateTime(now);
            //i.setAmount(price.multiply(BigDecimal.valueOf(i.getNumber())));
            shopCarMapper.updateShop(i);
            return;
        }
        Dish dish = dishMapper.getDish(dishId);
        price = dish.getPrice();
        name = dish.getName();
        image = dish.getImage();
        ShoppingCart build = ShoppingCart.builder().amount(price).dishId(Long.valueOf(dishId)).
                image(image).createTime(now).dishFlavor(dishFlavor).userId(currentId).
                number(number).name(name).build();
        shopCarMapper.insert(build);
    }

    /*private boolean extracted(ShoppingCartDTO shoppingCartDTO, ShoppingCart i, BigDecimal price, int number) {
        //如果口味变了，需要重新插入
        if(!i.getDishFlavor().equals(shoppingCartDTO.getDishFlavor())){
            i.setDishFlavor(shoppingCartDTO.getDishFlavor());
            i.setAmount(price);
            i.setNumber(number);
            i.setId(null);
            shopCarMapper.insert(i);
            return true;
        }
        return false;
    }*/
}
